--- 模块功能：新设备发送HTTP请求获取clientNo.
-- @author zhangfenghe
-- @module Tweeter.getNewClientNo
-- @license MIT
-- @copyright openLuat
-- @release 2022.04.26

module(..., package.seeall)

require "http"
require "nvm"
require "mqttTask"
require "mqttTaskQuakeInfoRel"
require "registerTask"
require "rtos"
require "logGenerator"

-- 初始化nvm获取配置参数
nvm.init("config.lua", true)
local clientNo, logInfo, errInfo, sysTime, regCenter = nvm.get("info")[1], "", "", "", nvm.get("regCenter")
local strLen = string.len
local ip, license, owner, type, groupId, clientId, uname, pwd, scope, mqtthost, topic, eqrTopic, answerTopic, port =
    nvm.get("centerConfig")[1], nvm.get("centerConfig")[2], nvm.get("centerConfig")[3],
    nvm.get("centerConfig")[4], nvm.get("centerConfig")[5],
    nvm.get("centerConfig")[6], nvm.get("centerConfig")[7], nvm.get("centerConfig")[8], nvm.get("centerConfig")[9],
    nvm.get("centerConfig")[10], nvm.get("centerConfig")[11], nvm.get("centerConfig")[12], nvm.get("centerConfig")[13],
    nvm.get("centerConfig")[14]

local function autoRegister()
    log.info("自动注册任务！！")
    log.info("regCenter", regCenter, "clientId", clientId)
    if regCenter == true then
        if clientId == nil or clientId == "" then
            if strLen(ip) > 0 and strLen(license) > 0 and strLen(owner) > 0 and strLen(type) > 0 then
                log.info("注册第二部", "ip" ~= ip and "license" ~= license and "owner" ~= owner and "type" ~= type)
                if "ip" ~= ip and "license" ~= license and "owner" ~= owner and "type" ~= type then
                    log.info("自动注册请求")
                    registerTask.RegistrationServiceTask()
                end
            end
        elseif strLen(clientId) > 0 and strLen(groupId) > 0 and strLen(uname) > 0 and strLen(pwd) > 0 then
            if (answerTopic == nil or answerTopic == "") and strLen(scope) > 0 then
                registerTask.AuthorizationApplicationUpdateServiceTask()
            end
        end
    end
end
--外接mqtt服务器连接任务
local function mqttTaskQuakeInfoRelAsk()
    mqttTaskQuakeInfoRel.startMQTTClient(clientNo)
end
--本司mqtt服务器连接任务
local function mqttTaskAsk()
    mqttTask.startMQTTClient(clientNo)
end
local function cbFnc(result, prompt, head, body)
    log.info("getNewClientNo.cbFnc", result, prompt)
    if result and head then
        for k, v in pairs(head) do
            log.info("getNewClientNo.cbFnc", k .. ": " .. v)
            logInfo = logInfo .. "[" .. sysTime .. "]" .. "[getNewClientNo.cbFnc:]" .. k .. ": " .. v .. "\n"
        end
    else
        errInfo = errInfo .. "[" .. sysTime .. "]" .. "[获取新设备号请求头错误！]\n"
    end
    if result and body then
        log.info("getNewClientNo.cbFnc", "bodyLen=" .. body:len())
        log.info("body", body)
        clientNo = body
        for i = 1, 10 do
            log.info("[clientNo:]" .. clientNo .. "\n")
        end
        logInfo = logInfo ..
            "[" .. sysTime .. "]" .. "[getNewClientNo.cbFnc:]" .. "bodyLen=" .. body:len() .. ",body=" .. body .. "\n"
        nvm.sett("info", 1, clientNo, "SVR")
        sys.taskInit(mqttTaskAsk)
        sys.taskInit(autoRegister)
        if regCenter then
            if uname ~= nil and pwd ~= nil  and mqtthost ~= nil and port ~= nil  then
                sys.taskInit(mqttTaskQuakeInfoRelAsk)
            end
        end
    else
        errInfo = errInfo .. "[" .. sysTime .. "]" .. "[获取新设备号请求体错误！]\n"
        logGenerator.generateLogTask(errInfo, "log")
    end
    log.info("getAndSetClientNo")
end
--获取并设置设备号
local function getAndSetClientNo()
    if "true" == regCenter then
        regCenter = true
    else
        regCenter = false
    end
    logInfo = ""
    sysTime = string.format("%04d-%02d-%02d %02d:%02d:%02d", os.date("*t").year,
        os.date("*t").month, os.date("*t").day, os.date("*t").hour, os.date("*t").min, os.date("*t").sec)
    if clientNo == "KS000807" or clientNo == nil then
        http.request("GET", "http://47.94.80.3:8080/quake/register?clientNo=", nil, nil, nil, nil, cbFnc)
    else
        --nvm.restore()
        sys.taskInit(mqttTaskAsk)
        sys.taskInit(autoRegister)
        if regCenter then
            log.info("登陆外系统条件：",uname ~= nil  and pwd ~= nil  and mqtthost ~= nil and port ~= nil)
            if uname ~= nil  and pwd ~= nil and mqtthost ~= nil and port ~= nil then
                sys.taskInit(mqttTaskQuakeInfoRelAsk)
            end
        end
    end
    logGenerator.generateLogTask(logInfo, "log")
end

getAndSetClientNo()
